﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
namespace girapong
{
    public class warning
    {

        public System.Windows.Shapes.Rectangle rMensaje { get; set; }
        public System.Windows.Controls.TextBlock text { get; set; }
        public String message { get; set; } // Message to be showed

        DispatcherTimer d; // Timer to perfom the warning

        // External window references
        private screen screenReference;
        private rally rallyReference;

        public warning(screen screenReference, rally rallyReference)
        {
            d = new DispatcherTimer();
            d.Interval = TimeSpan.FromMilliseconds(1);
            rMensaje = new System.Windows.Shapes.Rectangle();
            rMensaje.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 0, 0));
            rMensaje.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            rMensaje.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            rMensaje.Margin = new Thickness(100, -100, 0, 0);
            rMensaje.Opacity = 0.3;
            rMensaje.Width = 280;
            rMensaje.Height = 100;
            this.message = message;
            text = new System.Windows.Controls.TextBlock();
            text.FontSize = 30;
            text.FontWeight = FontWeights.Bold;
            text.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            text.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            text.Margin = new Thickness(130, -75, 0, 0);
            if (screenReference != null)
            {
                this.screenReference = screenReference;
                this.screenReference.LayoutRoot.Children.Add(rMensaje);
                this.screenReference.LayoutRoot.Children.Add(text);
            }
            else if (rallyReference != null)
            {
                this.rallyReference = rallyReference;
                this.rallyReference.LayoutRoot.Children.Add(rMensaje);
                this.rallyReference.LayoutRoot.Children.Add(text);

            }
        }
        public void open()
        {
            d.Tick += new EventHandler(expandA);
            d.Start();
        }
        public void close()
        {
            d.Tick += new EventHandler(expandC);
            d.Start();
        }
        public void setMessage(string message)
        {
            text.Text = message;
        }
        public void expandA(object sender, EventArgs e)
        {
            if (rMensaje.Margin.Top >= 200)
            {
                d.Stop();
                Thread.Sleep(1000);
                close();
            }
            else
            {
                double x = 0;
                if (rallyReference != null) x = (screen.WIDTH / 2);
                else x = (screen.HEIGHT / 2) - (rMensaje.Width / 2);
                rMensaje.Margin = new Thickness(100, rMensaje.Margin.Top + 20, 0, 0);
                text.Margin = new Thickness(100, rMensaje.Margin.Top + 20, 0, 0);
            }
        }
        public void expandC(object sender, EventArgs e)
        {
            if (rMensaje.Margin.Top <= -100)
            {
                d.Stop();
                if (screenReference != null) screenReference.playAll();
                else if (rallyReference != null)
                {
                    rallyReference.btnGuardarPuntuacion.Visibility = Visibility.Visible;
                    rallyReference.btnVolver.Visibility = Visibility.Visible;
                }
            }
            else
            {
                rMensaje.Margin = new Thickness(100, rMensaje.Margin.Top - 20, 0, 0);
                text.Margin = new Thickness(100, rMensaje.Margin.Top - 20, 0, 0);
            }
        }
    }
}
